<?php

namespace app\api\controller;

use app\common\controller\Ucenter;

/**
 * 首页接口
 */
class Index extends Ucenter
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $data['site_name']=config('site.name');
        $banner=Model('banner')->where(array('ishide'=>0))->select();
        $img=array();
        foreach($banner as $v){
            $p['id']=$v['id'];
            $p['img_url']=config('site_url').$v['path'];
            $p['link_url']=$v['linkurl'];
            $img[]=$p;
        }
        $data['adv_list']=$img;
        $data['kanchafei']='预约勘查费用：￥'.config('site.kanchafei');
        $data['index_tip']=config('site.index_tip');
        $this->success('请求成功',$data);
    }
    private function get_goods($mobile){
        $order_list=Model('orders_input')->where(array('kehu_mobile'=>$mobile))->select();
        $goods=array();
        if(!empty($order_list)){
            $oids=array();
            $order_sn=array();
            foreach($order_list as $o){
                $oids[]=$o['id'];
                $order_sn[$o['id']]=$o['order_sn'];
            }
            $oids=implode(',',$oids);
            $where=array();
            $where['order_id']=array('in',$oids);
            $goods_list=Model('orders_input_item')->where($where)->select();
            $goods=array();
            $zidian=base_list();
            foreach($goods_list as $v){
                $p=array();
                $p['id']=$v['good_id'];
                $p['title']=$zidian[$v['good_id']];
                $p['order_id']=$v['order_id'];
                $p['order_sn']=$order_sn[$v['order_id']] ?? '';
                $p['good_num']=$v['good_num'];
                $p['good_price']=$v['good_price'];
                $goods[]=$p;
            }
        }
        return $goods;
    }
    public function zidian(){
        $list=Model('zidian')->where(array('cid'=>0))->select();
        $data=array();
        foreach($list as $v){
            $arr['name']=$v['title'];
            $arr['select_name']=$v['fname'];
            $sub=Model('zidian')->where(array('cid'=>$v['id']))->select();
            if($v['fname']=='product_test'){
                $sub=array();
                if(strlen($this->uinfo['mobile'])>3){
                    $sub=$this->get_goods($this->uinfo['mobile']);
                }
            }else{
                if(!empty($sub)){
                    foreach($sub as $k=>$b){
                        $title=str_replace('&amp;','&',$b['title']);
                        $sub[$k]['title']=htmlspecialchars_decode($title);
                    }
                }
            }
            $arr['sub']=$sub;
            $data[$v['fname']]=$arr;
        }
        $data['kanchafei']='0.01';config('site.kanchafei');
        $this->success('1',$data);
    }
}